﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.IO;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SF.Utilities;
using SF.Import;
using System.Data;
using System.Reflection;

namespace SF.Test
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestMethod1()
        {
            ////typeof(UserState); 就是java的 .class
            ////跟java写法一样，实在要说复杂，就是多了一个 强转（UserState）
            //UserState us = (UserState)Enum.Parse(typeof(UserState), "Normal");

            ////从语法角度来说，我也认为Enum.Parse应该支持泛型，这点略有欠缺（不知道为什么不实现。。但实现真的很容易），所以我这样写扩展：
            //UserState us2 = EnumExt.Parse<UserState>("Normal"); //这样就和java的写法一样了

            //Console.WriteLine(us.ToString());


            int? a = 1;
            Nullable<int> b = 2;
            int c = 3;
            int? d = null;

            Type t1 = a.GetType(), t2 = b.GetType(), t3 = c.GetType();
            Console.WriteLine(t1.FullName);
            Console.WriteLine(t2.FullName);
            Console.WriteLine(t3.FullName);
            Console.WriteLine(t1 == t2);
            Console.WriteLine(t2 == t3);
            Console.WriteLine(t1 == t3);
            Console.WriteLine(typeof(int).FullName);
            Console.WriteLine(typeof(int?).FullName);

        }

        [TestMethod]
        public void test2()
        {
            string path = "C:\\Users\\溪林\\Desktop\\测试1.xls";

            FileInfo file = new FileInfo(path);

            Console.WriteLine(file == null ? 0 : 1);

            var data = NPOIImport.GetExcelData(file, true);
            foreach (var item in data)
            {
                Console.WriteLine("sheet名：" + item.Key);
                Console.WriteLine("内如如下：");
                int index = 1;
                foreach (var item2 in item.Value)
                {
                    Console.WriteLine("第" + index + "行：");
                    foreach (var item3 in item2)
                    {
                        Console.WriteLine("列名：" + item3.Key + "，列值：" + item3.Value + "。");
                    }
                    index++;
                }
            }
        }

        [TestMethod]
        public void test3()
        {
            string path = "C:\\Users\\溪林\\Desktop\\测试2.xlsx";

            FileInfo file = new FileInfo(path);

            Console.WriteLine(file == null ? 0 : 1);

            var data = NPOIImport.test1(file);
            foreach (DataTable item in data.Tables)
            {
                Console.WriteLine("sheet名：" + item.TableName);
                Console.WriteLine("数据如下：");
                int index = 1;
                foreach (DataRow item2 in item.Rows)
                {
                    Console.WriteLine("第" + index + "行：");
                    foreach (DataColumn item3 in item.Columns)
                    {
                        string value = item2[item3.ColumnName].ToString();
                        Console.WriteLine("列名：[" + item3.ColumnName + "]，列值：[" + value + "]，长度-" + value.Length);
                    }
                    index++;
                }
            }
        }

        [TestMethod]
        public void test4()
        {
            T1 t1 = new T1()
            {
                id = "1",
                name = "t1",
                t2s = new T2[] { new T2() { id = "10", name = "t21" }, new T2() { id = "20", name = "t22" } }
            };

            this.get<T1, T2>(t1);
        }

        private void get<T, DetailT1>(T obj1)
        {
            Type t1 = typeof(T);
            PropertyInfo[] props = t1.GetProperties();
            foreach (var item in props)
            {
                string itemName = item.Name;
                Type itemType = item.PropertyType;
                if (itemType.IsArray)
                {
                    Type arrayElementType = itemType.GetElementType();
                    Object arrayValue = item.GetMethod.Invoke(obj1, null);
                    if (typeof(DetailT1) == arrayElementType)
                    {
                        DetailT1[] dt1s = (DetailT1[])arrayValue;
                        foreach (DetailT1 dt in dt1s)
                        {
                            string s1 = dt.ToString();
                        }
                    }
                }
            }
        }

        public class T1
        {
            public string id { get; set; }

            public string name { get; set; }

            public T2[] t2s { get; set; }
        }

        public class T2
        {
            public string id { get; set; }

            public string name { get; set; }
        }

        public class Student
        {
            public string Name { get; set; }

            public bool Sex { get; set; }

            public string BarSize { get; set; }

            public int Height { get; set; }
        }

        [TestMethod]
        public void test1111()
        {
            List<Student> students = new List<Student>() { 
                new Student{ Name="翠花1", Sex =false, BarSize = "32C", Height = 170 },
                new Student{ Name="翠花2", Sex =false, BarSize = "34D", Height = 165 },
                new Student{ Name="翠花3", Sex =false, BarSize = "32C", Height = 163 },
                new Student{ Name="翠花3", Sex =false, BarSize = "32C", Height = 163 }
            };
            var r1 = students.Where(w => !w.Sex).GroupBy(g => g.BarSize);
            r1.ForEach(f => {
                Console.WriteLine("分组字段：" + f.Key);
                Console.WriteLine("具体数据：");
                f.ForEach(ff => {
                    Console.WriteLine("【姓名】" + ff.Name);
                });
            });

            var r2 = students.Distinct();
            r2.ForEach(f => {
                Console.WriteLine(f.Name);
            });
        }
    }
}
